package com.fsq.xmfsubdomain;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class Level4Brute extends Task<String> {

	private CountDownLatch countDownLatch;
	private LinkedBlockingQueue<String> queue;
	private Set<InetAddress> blacklist;
	private TreeView<String> id_treeview;
	private TableView<Row> id_tableview;
	private String domain;
	private Label id_status;
	private Set<String> ipList;
	private Set<Record> level4RecordSet;
	private TreeItem<String> tiLevel4;

	public Level4Brute(CountDownLatch countDownLatch, LinkedBlockingQueue<String> queue, Set<InetAddress> blacklist,
					   TreeView<String> id_treeview, TableView<Row> id_tableview, String domain, Label id_status,
					   Set<String> ipList, Set<Record> level4RecordSet, TreeItem<String> tiLevel4) {
		super();
		this.countDownLatch = countDownLatch;
		this.queue = queue;
		this.blacklist = blacklist;
		this.id_treeview = id_treeview;
		this.id_tableview = id_tableview;
		this.domain = domain;
		this.id_status = id_status;
		this.ipList = ipList;
		this.level4RecordSet = level4RecordSet;
		this.tiLevel4 = tiLevel4;
	}

	@Override
	protected String call() throws Exception {

		try {
			ObservableList<Row> items = id_tableview.getItems();
			String domainname;
			while ((domainname = queue.poll(5, TimeUnit.SECONDS)) != null) {
				String dn = domainname;
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						id_status.setText("正在爆破四级域名" + dn);

					}
				});

				System.out.println("正在爆破四级域名：" + domainname);
				Record[] records = new Lookup(domainname).run();
				if (records != null) {
					for (Record record : records) {
						InetAddress address = ((ARecord) record).getAddress();
						String ip = address.getHostAddress();
						// 此域名对应的ip不在黑名单则加入到总列表
						if (!blacklist.contains(address) && level4RecordSet.add(record)) {

							ipList.add(ip);
							Row row = new Row(domain, domainname, ip, "字典爆破", "4", "A记录");
							TreeItem<String> tiRecord = new TreeItem<String>(domainname);
							Platform.runLater(new Runnable() {

								@Override
								public void run() {
									tiLevel4.getChildren().add(tiRecord);
									items.add(row);
								}
							});
						}
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 破解线程计数减1
			countDownLatch.countDown();
			System.out.println("四级爆破剩余线程:" + countDownLatch.getCount());
		}
		return null;

	}

}
